// proxySubAgManage.js
const app = getApp()
var util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:{
      searchAddShow: true,
      BtnTxt:'添加代理',
      searchTip: '可输入ID、姓名等',
      searchValue: '',
    },

    list:[],
    pagenum:1
  },
  // 添加代理
  addTap:function(){
    wx.navigateTo({
      url: '../../pages/proxySubAgAdd/proxySubAgAdd',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.manageList('defalut');
    wx.stopPullDownRefresh()
  },
  searchChange:function(event){
    var detail = 'search.searchValue'
    this.setData({
      [detail]: event.detail
    })
  },
  manageList:function(type){
    var that = this;
    if(type == 'search'){
      that.setData({list:[]})
    }
    var data = { page: that.data.pagenum, input: that.data.search.searchValue };
    app.post("agent/agentChild", data).then((res) => {
      if (res.code == 200) {
        var list = that.data.list;
        var dataArr = res.list;
        list = list.concat(dataArr)
        that.setData({
          list:list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);//错误提示信息
    })
  },
  onSearch:function() {
    this.manageList('search');
  },
  ToDetailTap:function(e){
    var index = e.currentTarget.dataset.index;
    var that = this;
    var list = that.data.list;
    wx.navigateTo({
      url: '../../pages/proxySubAgDetail/proxySubAgDetail?detail=' + JSON.stringify(list[index]),
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  onShow: function () {
    // this.setData({
    //   list: [],
    //   pagenum: 1
    // })
    // this.onLoad();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      pagenum: 1,
      ['search.searchValue']: ''
    })
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    that.manageList();//重新调用请求获取下一页数据
  }
})